package com.example.dingdong.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.example.dingdong.daomain.RouterEntity;

/**
 * @ClassName:  RouterDao
 * @Description: 路由实体Dao
 * @Author chengwenxing
 * @DateTime 2021-03-31 
 */
public interface RouterRepository  extends JpaRepository<RouterEntity,Long>{
	/**
	 * 查询路由实体
	 */
	RouterEntity findByIdAndDelFlag(int id, boolean delFlag);

	/**
	 * 查找路由集合
	 */
	@Query(nativeQuery = true, value = "SELECT * FROM router_entity WHERE id IN ?1 AND del_flag = ?2 ")
	List<RouterEntity> findByIdInDelFlag(List<Integer> routerList, boolean delFlag);

	/**
	 * 查询所有路由
	 */
	List<RouterEntity> findByDelFlag(boolean delFlag);

	/**
	 * 查询所有父级路由
	 */
	@Query(nativeQuery = true, value = "SELECT * FROM router_entity WHERE parent_id IS NULL AND del_flag = true")
	List<RouterEntity> findParentRouter();

	/**
	 * 更新路由实体
	 */
	@Modifying
	@Query(nativeQuery = true, value = "UPDATE router_entity SET path=?2, component=?3, name=?4, icon=?5, strict=?6, exact=?7, redirect=?8 WHERE id=?1")
	void updateRouter(int id, String path, String component, String name, String icon, boolean strict, boolean exact,
			String redirect);

	/**
	 * 更新换绑父级路由
	 */
	@Modifying
	@Query(nativeQuery = true, value = "UPDATE router_entity SET parent_id = IF(?1 = -1, NULL, ?1), path=?3, sort=?4, component=?5, name=?6, icon=?7, strict=?8, exact=?9, redirect=?10 WHERE id=?2")
	void updateAndChangeBindRouter(int parentId, int id, String path, int sort, String component, String name,
			String icon, boolean strict, boolean exact, String redirect);
}
